package com.asset.dispoal.menu.service.commom;

import com.asset.dispoal.menu.dto.response.GbMenuResponse;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class MenuList {
    public static Result<List<GbMenuResponse>> getListResult(List<GbMenuResponse> gbMenuResponses) {
        for (GbMenuResponse parent : gbMenuResponses) {
            for (GbMenuResponse menu : gbMenuResponses) {
                if (parent.getId().equals(menu.getParentId())) {
                    parent.getGbMenuResponse().add(menu);
                    parent.getGbMenuResponse().sort(Comparator.comparingInt(GbMenuResponse::getSorted));
                }
            }
        }
        final List<GbMenuResponse> gbMenuResponseStream = gbMenuResponses.stream().filter(item -> StringUtil.isBlank(item.getParentId())).sorted(Comparator.comparingInt(GbMenuResponse::getSorted)).collect(Collectors.toList());
        return Result.ok(gbMenuResponseStream);
    }
}
